package com.android.commcount;

import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.android.baselibrary.Lib_Base;
import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.AndPermisionUtil;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.tool.Log;
import com.android.commcount.bean.CommCount_Type;
import com.android.commcount.manager.API_Manager;
import com.android.commcount.ui.activity.CustomCamare1Activity;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import org.litepal.LitePal;

import java.util.List;

public class Lib_CommCount {

    public static void init(Application context) {
        Log.i("Lib_CommCount 加载 ");
        Lib_CommCount.getInstance().mContext = context;

        Lib_Base.init(context);
        LitePal.initialize(context);
    }

    private Application mContext;
    private static Lib_CommCount libCommCount = null;

    public static Lib_CommCount getInstance() {
        if (libCommCount == null) {
            libCommCount = new Lib_CommCount();
        }
        return libCommCount;
    }

    public Application getContext() {
        return mContext;
    }


    //获取模板
    private void template(Context context, CommCallBack callBack) {
        Log.i("接口获取分类");
        API_Manager.template(mContext, new OkHttpCallBack<BaseResponce<List<CommCount_Type>>>() {
            @Override
            public void onSuccess(BaseResponce<List<CommCount_Type>> baseResponce) {
                Log.i("分类存本地");
                List<CommCount_Type> typeList = baseResponce.getData();
                for (int i = 0; i < typeList.size(); i++) {
                    if (i == 0) {
                        typeList.get(i).isShowOnHome = 1;
                    }
                    typeList.get(i).sort = i;
                    typeList.get(i).save();
                }
                if (callBack != null) {
                    callBack.onResult(null);
                }
            }

            @Override
            public void onFailure(BaseResponce<List<CommCount_Type>> baseResponce) {
            }
        });
    }

    /**
     * 获取类型列表
     */
    public void getTypeList(Context context, CommCallBack callBack) {
        List<CommCount_Type> typeList = LitePal.findAll(CommCount_Type.class);
        if (typeList != null && typeList.size() > 0) {
            callBack.onResult(typeList);
        } else {
            template(context, new CommCallBack() {
                @Override
                public void onResult(Object obj) {
                    getTypeList(context, callBack);
                }
            });
        }
    }

    /**
     * 获取显示在首页的类型
     */
    public void getType_Home(Context context, CommCallBack callBack) {
        getTypeList(context, new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                List<CommCount_Type> typeList = (List<CommCount_Type>) obj;
                for (CommCount_Type commCount_type : typeList) {
                    if (commCount_type.isShowOnHome == 1) {
                        if (callBack != null) callBack.onResult(commCount_type);
                    }
                }
            }
        });
    }

    /**
     * 开始拍照
     *
     * @param mContext
     * @param commCount_type
     */
    public static void startCount(Context mContext, CommCount_Type commCount_type) {
        if (AndPermission.hasPermissions(mContext, Permission.Group.CAMERA)) {
            toCamera(mContext, commCount_type);
        } else {
            AndPermisionUtil.requstPermision(mContext, new AndPermisionUtil.PermisionCallBack() {
                @Override
                public void onGranted() {
                    toCamera(mContext, commCount_type);
                }

                @Override
                public void onDenied() {
                    CommToast.showToast(mContext, "相机权限被拒绝，无法使用相机");
                }
            }, Permission.Group.CAMERA);
        }
    }

    private static void toCamera(Context mContext, CommCount_Type commCount_type) {
        Intent intent = new Intent(mContext, CustomCamare1Activity.class);
        intent.putExtra("commCount_type", commCount_type);
        mContext.startActivity(intent);
//        getActivity().overridePendingTransition(R.anim.main_activity_in, R.anim.anim_activityout);
    }
}
